package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wk
 * @create 2021-08-24 18:16
 */
@RestController
//@CrossOrigin
@Slf4j
@RequestMapping("/admin/product")
@Api(tags = "管理系统")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    // http://localhost/admin/product/getCategory1
    @ApiOperation("获取一级分类")
    @GetMapping("/getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> baseCategory1List = managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    // http://api.gmall.com/admin/product/getCategory2/{category1Id}
    @ApiOperation("获取二级分类")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(
            @ApiParam(value = "一级分类id", required = true)
            @PathVariable Long category1Id) {
        List<BaseCategory2> baseCategory2List = managerService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    @ApiOperation("获取三级分类")
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(
            @ApiParam(value = "二级分类id", required = true)
            @PathVariable Long category2Id) {
        List<BaseCategory3> baseCategory3List = managerService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    // http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @ApiOperation("根据分类id获取平台属性及属性值集合")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @ApiParam(value = "一级分类id", required = true)
            @PathVariable Long category1Id,
            @ApiParam(value = "二级分类id", required = true)
            @PathVariable Long category2Id,
            @ApiParam(value = "三级分类id", required = true)
            @PathVariable Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    // http://api.gmall.com/admin/product/saveAttrInfo
    @ApiOperation("添加平台属性及属性值")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(
            @ApiParam(value = "平台属性对象", required = true)
            @RequestBody BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() == null) {
            managerService.saveAttrInfo(baseAttrInfo);
        } else {
            managerService.updateAttrInfo(baseAttrInfo);
        }

        return Result.ok();
    }

    // http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @ApiOperation("根据平台属性ID获取平台属性")
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(
            @ApiParam(value = "平台属性ID", required = true)
            @PathVariable Long attrId) {
        List<BaseAttrValue> baseAttrValueList = managerService.getAttrValueList(attrId);
        if (baseAttrValueList != null) {
            return Result.ok(baseAttrValueList);
        } else {
            return Result.fail();
        }
    }

    // http://api.gmall.com/admin/product/baseTrademark/{page}/{limit}
    @GetMapping("/baseTrademark/{page}/{limit}")
    @ApiOperation("获取品牌分页列表")
    public Result baseTrademark(
            @ApiParam(value = "当前页", required = true)
            @PathVariable Integer page,
            @ApiParam(value = "每页显示数量", required = true)
            @PathVariable Integer limit) {
        IPage<BaseTrademark> iPage = managerService.baseTrademark(page, limit);
        return Result.ok(iPage);
    }

    // http://localhost:8206/admin/product/baseTrademark/save
    @PostMapping("/baseTrademark/save")
    @ApiOperation("添加品牌")
    public Result save(
            @ApiParam(value = "品牌对象", required = true)
            @RequestBody BaseTrademark baseTrademark) {
        managerService.save(baseTrademark);
        return Result.ok();
    }

    // http://localhost:8206/admin/product/baseTrademark/get/7
    @GetMapping("/baseTrademark/get/{id}")
    @ApiOperation("根据id修改品牌")
    public Result get(
            @ApiParam(value = "品牌id", required = true)
            @PathVariable Long id) {
        BaseTrademark baseTrademark = managerService.get(id);
        return Result.ok(baseTrademark);
    }

    // http://localhost:8206/admin/product/baseTrademark/update
    @PutMapping("/baseTrademark/update")
    @ApiOperation("更新品牌")
    public Result update(
            @ApiParam(value = "品牌对象", required = true)
            @RequestBody BaseTrademark baseTrademark) {
        managerService.update(baseTrademark);
        return Result.ok();
    }

    // http://localhost:8206/admin/product/baseTrademark/remove/6
    @DeleteMapping("/baseTrademark/remove/{id}")
    @ApiOperation("删除品牌")
    public Result remove(
            @ApiParam(value = "品牌id", required = true)
            @PathVariable Long id) {
        managerService.remove(id);
        return Result.ok();
    }

    // http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
    @ApiOperation("根据三级id查询spu列表")
    @GetMapping("/{page}/{limit}")
    public Result getSpuPage(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Long category3Id) {

        IPage iPage = managerService.getSpuPage(page, limit, category3Id);
        return Result.ok(iPage);
    }

    // http://api.gmall.com/admin/product/baseTrademark/getTrademarkList
    @ApiOperation("获取品牌属性")
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    // http://api.gmall.com/admin/product/baseSaleAttrList
    @ApiOperation("获取品牌属性")
    @GetMapping("/baseSaleAttrList")
    public Result getSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = managerService.getSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    // http://api.gmall.com/admin/product/saveSpuInfo
    @ApiOperation("保存SPU")// spu标准化产品单元
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    // http://api.gmall.com/admin/product/list/1/10
    @ApiOperation("获取sku分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result skuPageList(@PathVariable Integer page, @PathVariable Integer limit) {
        IPage<SkuInfo> iPage = managerService.skuPageList(page, limit);
        return Result.ok(iPage);
    }

    // http://api.gmall.com/admin/product/spuImageList/{spuId}
    @ApiOperation("根据spu id 获取图片列表")
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImageList = managerService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    // http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
    @ApiOperation("根据spu id 获取销售属性")
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = managerService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    // http://api.gmall.com/admin/product/saveSkuInfo
    @ApiOperation("添加sku 库存量")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    // http://api.gmall.com/admin/product/onSale/{skuId}
    @ApiOperation("上架")
    @PutMapping("/onSale/{skuId}")
    public Result onSave(@PathVariable Long skuId){
        managerService.onSave(skuId);
        return Result.ok();
    }
    // http://api.gmall.com/admin/product/cancelSale/{skuId}
    @ApiOperation("下架")
    @PutMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
